package com.shop2cn.iapi.member.testcase;

import java.math.BigDecimal;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.member.parameter.CreateMemberBean;
import com.shop2cn.iapi.member.parameter.PortionRefundBean;
import com.shop2cn.iapi.member.service.PortionRefundCall;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.GodMemberService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmember.MemberPoints;
import com.ymttest.database.model.godmember.PointsEntry;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.database.sqlwapper.godmember.GodMemberWapper;
/**
*接收订单取消消息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_PortionRefund{
	private static PortionRefundBean portionRefundBean;
	private static PortionRefundCall portionRefundCall;
	GodMemberWapper GodMemberWapper;
	private static Integer shopid ;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("接收订单取消消息");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {
		portionRefundBean=new PortionRefundBean();
		portionRefundCall=new PortionRefundCall();
		GodMemberWapper=new com.ymttest.database.sqlwapper.godmember.GodMemberWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(portionRefundCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_PortionRefund_001() {
		Logger.start(true,"接收订单取消消息成功,积分减少");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			Long orderid=Long.valueOf(TestDataManager.getRandomNum(10));
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			String sqOrderRmbAmount="101";
			GodMemberService.processOrderPay(userId, shopid, orderid, sqOrderRmbAmount);
			portionRefundBean.setUserId(userId);
			portionRefundBean.setOrderId(orderid);
			portionRefundBean.setSellerId(shopid);
			portionRefundBean.setBizNo(YMTDateUtil.getBeforeOrNextDay(0, "yyyyMMddHHmmss")+TestDataManager.getRandomNum(4));
			portionRefundCall.setData(portionRefundBean);
			portionRefundCall.callService();			
			Logger.verifyEquals("ok", portionRefundCall.getReturnData(), "验证返回");
			
			Thread.sleep(5000);
			MemberPoints mpdb = GodMemberWapper.selectMemberPointsByMemberId(memid).get(0);
			Logger.debug(FormatUtil.beanToGSONString(mpdb));
//			{"memberId":"HY20200521001322","shopId":500962122,"availablePoints":10,"totalPoints":10,"orderNum":0,"orderAmt":0.00,"lastMonthOrderAmt":0.00,"lastOrderAmt":0.00,"lastOrderId":0,"createTime":"2020-05-21T18:45:52","updateTime":"2020-05-21T18:45:52"}
			Logger.verifyEquals(0,mpdb.getAvailablePoints(), "验证getAvailablePoints");
			Logger.verifyEquals(shopid,mpdb.getShopId(), "验证getShopId");
			Logger.verifyEquals(0,mpdb.getTotalPoints(), "验证getTotalPoints");
			Logger.verifyNotNull(mpdb.getCreateTime(), "验证getCreateTime");
			List<PointsEntry> Entrylist = GodMemberWapper.selectPointsEntryByMemberid(memid);
			Logger.debug(FormatUtil.beanToGSONString(Entrylist));
			PointsEntry entrydb = Entrylist.stream().filter(x->{return x.getBizType().equals(110000);}).findFirst().get();
//			[{"entryId":63,"memberId":"HY20200521001326","bizType":200000,"bizId":"c62523d2d75e466493ef30c28e58873e","changePoints":10,"points":10,"pointsTime":"2020-05-21T18:50:54","operator":"主账号","remark":"test932","createTime":"2020-05-21T18:50:54","updateTime":"2020-05-21T18:50:54"}]
			Logger.verifyEquals(-5,entrydb.getChangePoints(), "验证ChangePoints");
			Logger.verifyEquals(0,entrydb.getPoints(),  "验证points");
			Logger.verifyEquals("系统",entrydb.getOperator(),  "验证Operator");
			Logger.verifyEquals("退款扣积分",entrydb.getRemark(), "验证remark");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	@Ignore
	//取消是跟着订单走的
	public void Tc_PortionRefund_002() {
		Logger.start(true,"店铺不存在,不处理");
		try {
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	@Ignore
	//取消是跟着订单走的
	public void Tc_PortionRefund_003() {
		Logger.start(true,"用户不存在,不处理");
		try {
			//TODO 补全case代码
			org.junit.Assert.fail();
			portionRefundCall.setData(portionRefundBean);
			portionRefundCall.callService();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_PortionRefund_004() {
		Logger.start(true,"订单号不存在,不处理");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			Long orderid=Long.valueOf(TestDataManager.getRandomNum(10));
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			String sqOrderRmbAmount="101";
			GodMemberService.processOrderPay(userId, shopid, orderid, sqOrderRmbAmount);
			portionRefundBean.setUserId(userId);
			portionRefundBean.setOrderId(orderid+100);
			portionRefundBean.setSellerId(shopid);
			portionRefundBean.setBizNo(YMTDateUtil.getBeforeOrNextDay(0, "yyyyMMddHHmmss")+TestDataManager.getRandomNum(4));
			portionRefundCall.setData(portionRefundBean);
			portionRefundCall.callService();			
			Logger.verifyEquals("ok", portionRefundCall.getReturnData(), "验证返回");
			
			MemberPoints mpdb = GodMemberWapper.selectMemberPointsByMemberId(memid).get(0);
			Logger.debug(FormatUtil.beanToGSONString(mpdb));
//			{"memberId":"HY20200521001322","shopId":500962122,"availablePoints":10,"totalPoints":10,"orderNum":0,"orderAmt":0.00,"lastMonthOrderAmt":0.00,"lastOrderAmt":0.00,"lastOrderId":0,"createTime":"2020-05-21T18:45:52","updateTime":"2020-05-21T18:45:52"}
			Logger.verifyEquals(5,mpdb.getAvailablePoints(), "验证getAvailablePoints");
			Logger.verifyEquals(shopid,mpdb.getShopId(), "验证getShopId");
			Logger.verifyEquals(5,mpdb.getTotalPoints(), "验证getTotalPoints");
			Logger.verifyNotNull(mpdb.getCreateTime(), "验证getCreateTime");
			List<PointsEntry> Entrylist = GodMemberWapper.selectPointsEntryByMemberid(memid);
			Logger.debug(FormatUtil.beanToGSONString(Entrylist));
			boolean isPresent = Entrylist.stream().filter(x->{return x.getBizType().equals(110000);}).findFirst().isPresent();
			Logger.verifyEquals(false, isPresent, "订单号错误流水不存在");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_PortionRefund_008() {
		Logger.start(true,"订单金额不足最小规则,验证积分");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			Long orderid=Long.valueOf(TestDataManager.getRandomNum(10));
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			String sqOrderRmbAmount="99";
			GodMemberService.processOrderPay(userId, shopid, orderid, sqOrderRmbAmount);
			portionRefundBean.setUserId(userId);
			portionRefundBean.setOrderId(orderid);
			portionRefundBean.setSellerId(shopid);
			portionRefundBean.setBizNo(YMTDateUtil.getBeforeOrNextDay(0, "yyyyMMddHHmmss")+TestDataManager.getRandomNum(4));
			portionRefundCall.setData(portionRefundBean);
			portionRefundCall.callService();			
			Logger.verifyEquals("ok", portionRefundCall.getReturnData(), "验证返回");
			
			MemberPoints mpdb = GodMemberWapper.selectMemberPointsByMemberId(memid).get(0);
			Logger.debug(FormatUtil.beanToGSONString(mpdb));
//			{"memberId":"HY20200521001322","shopId":500962122,"availablePoints":10,"totalPoints":10,"orderNum":0,"orderAmt":0.00,"lastMonthOrderAmt":0.00,"lastOrderAmt":0.00,"lastOrderId":0,"createTime":"2020-05-21T18:45:52","updateTime":"2020-05-21T18:45:52"}
			Logger.verifyEquals(0,mpdb.getAvailablePoints(), "验证getAvailablePoints");
			Logger.verifyEquals(shopid,mpdb.getShopId(), "验证getShopId");
			Logger.verifyEquals(0,mpdb.getTotalPoints(), "验证getTotalPoints");
			Logger.verifyNotNull(mpdb.getCreateTime(), "验证getCreateTime");
			List<PointsEntry> Entrylist = GodMemberWapper.selectPointsEntryByMemberid(memid);
			Logger.debug(FormatUtil.beanToGSONString(Entrylist));
			boolean isPresent = Entrylist.stream().filter(x->{return x.getBizType().equals(110000);}).findFirst().isPresent();
			Logger.verifyEquals(false, isPresent, "订单号不加积分流水不存在");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_PortionRefund_005() {
		Logger.start(true,"订单金额为门槛的多倍,验证积分");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			Long orderid=Long.valueOf(TestDataManager.getRandomNum(10));
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			String sqOrderRmbAmount="1000";
			GodMemberService.processOrderPay(userId, shopid, orderid, sqOrderRmbAmount);
			portionRefundBean.setUserId(userId);
			portionRefundBean.setOrderId(orderid);
			portionRefundBean.setSellerId(shopid);
			portionRefundBean.setBizNo(YMTDateUtil.getBeforeOrNextDay(0, "yyyyMMddHHmmss")+TestDataManager.getRandomNum(4));
			portionRefundCall.setData(portionRefundBean);
			portionRefundCall.callService();			
			Logger.verifyEquals("ok", portionRefundCall.getReturnData(), "验证返回");
			
			MemberPoints mpdb = GodMemberWapper.selectMemberPointsByMemberId(memid).get(0);
			Logger.debug(FormatUtil.beanToGSONString(mpdb));
//			{"memberId":"HY20200521001322","shopId":500962122,"availablePoints":10,"totalPoints":10,"orderNum":0,"orderAmt":0.00,"lastMonthOrderAmt":0.00,"lastOrderAmt":0.00,"lastOrderId":0,"createTime":"2020-05-21T18:45:52","updateTime":"2020-05-21T18:45:52"}
			Logger.verifyEquals(0,mpdb.getAvailablePoints(), "验证getAvailablePoints");
			Logger.verifyEquals(shopid,mpdb.getShopId(), "验证getShopId");
			Logger.verifyEquals(0,mpdb.getTotalPoints(), "验证getTotalPoints");
			Logger.verifyNotNull(mpdb.getCreateTime(), "验证getCreateTime");
			List<PointsEntry> Entrylist = GodMemberWapper.selectPointsEntryByMemberid(memid);
			Logger.debug(FormatUtil.beanToGSONString(Entrylist));
			PointsEntry entrydb = Entrylist.stream().filter(x->{return x.getBizType().equals(110000);}).findFirst().get();
//			[{"entryId":63,"memberId":"HY20200521001326","bizType":200000,"bizId":"c62523d2d75e466493ef30c28e58873e","changePoints":10,"points":10,"pointsTime":"2020-05-21T18:50:54","operator":"主账号","remark":"test932","createTime":"2020-05-21T18:50:54","updateTime":"2020-05-21T18:50:54"}]
			Logger.verifyEquals(-50,entrydb.getChangePoints(), "验证ChangePoints");
			Logger.verifyEquals(0,entrydb.getPoints(),  "验证points");
			Logger.verifyEquals("系统",entrydb.getOperator(),  "验证Operator");
			Logger.verifyEquals("退款扣积分",entrydb.getRemark(), "验证remark");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_PortionRefund_007() {
		Logger.start(true,"商家没有设置过积分规则");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			Long orderid=Long.valueOf(TestDataManager.getRandomNum(10));
//			GodMemberService.mchSetOpenPoints(GodMemberService.initSetPointsBean(shopid));
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			String sqOrderRmbAmount="101";
			GodMemberService.processOrderPay(userId, shopid, orderid, sqOrderRmbAmount);
			portionRefundBean.setUserId(userId);
			portionRefundBean.setOrderId(orderid);
			portionRefundBean.setSellerId(shopid);
			portionRefundBean.setBizNo(YMTDateUtil.getBeforeOrNextDay(0, "yyyyMMddHHmmss")+TestDataManager.getRandomNum(4));
			portionRefundCall.setData(portionRefundBean);
			portionRefundCall.callService();			
			Logger.verifyEquals("ok", portionRefundCall.getReturnData(), "验证返回");
			
			MemberPoints mpdb = GodMemberWapper.selectMemberPointsByMemberId(memid).get(0);
			Logger.debug(FormatUtil.beanToGSONString(mpdb));
//			{"memberId":"HY20200521001322","shopId":500962122,"availablePoints":10,"totalPoints":10,"orderNum":0,"orderAmt":0.00,"lastMonthOrderAmt":0.00,"lastOrderAmt":0.00,"lastOrderId":0,"createTime":"2020-05-21T18:45:52","updateTime":"2020-05-21T18:45:52"}
			Logger.verifyEquals(0,mpdb.getAvailablePoints(), "验证getAvailablePoints");
			Logger.verifyEquals(shopid,mpdb.getShopId(), "验证getShopId");
			Logger.verifyEquals(0,mpdb.getTotalPoints(), "验证getTotalPoints");
			Logger.verifyNotNull(mpdb.getCreateTime(), "验证getCreateTime");
			List<PointsEntry> Entrylist = GodMemberWapper.selectPointsEntryByMemberid(memid);
			Logger.debug(FormatUtil.beanToGSONString(Entrylist));
			Logger.verifyEquals(false,Entrylist.stream().filter(x->{return x.getBizType().equals(110000);}).findFirst().isPresent(),"验证数据不存在");
//			[{"entryId":63,"memberId":"HY20200521001326","bizType":200000,"bizId":"c62523d2d75e466493ef30c28e58873e","changePoints":10,"points":10,"pointsTime":"2020-05-21T18:50:54","operator":"主账号","remark":"test932","createTime":"2020-05-21T18:50:54","updateTime":"2020-05-21T18:50:54"}]
//			Logger.verifyEquals(-5,entrydb.getChangePoints(), "验证ChangePoints");
//			Logger.verifyEquals(0,entrydb.getPoints(),  "验证points");
//			Logger.verifyEquals("系统",entrydb.getOperator(),  "验证Operator");
//			Logger.verifyEquals("退款扣积分",entrydb.getRemark(), "验证remark");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_PortionRefund_009() {
		Logger.start(true,"接收订单取消消息成功,部分退款、积分减少");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			Long orderid=Long.valueOf(TestDataManager.getRandomNum(10));
			GodMemberService.mchSetOpenPoints(shopid, true);
			GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
			String memid = GodMemberService.creatememberCall(createMem);
			String sqOrderRmbAmount="101";
			GodMemberService.processOrderPay(userId, shopid, orderid, sqOrderRmbAmount);
			portionRefundBean.setUserId(userId);
			portionRefundBean.setOrderId(orderid);
			portionRefundBean.setSellerId(shopid);
			portionRefundBean.setBizNo(YMTDateUtil.getBeforeOrNextDay(0, "yyyyMMddHHmmss")+TestDataManager.getRandomNum(4));
			portionRefundBean.setRefundAmountRmb(new BigDecimal(10));
			portionRefundCall.setData(portionRefundBean);
			portionRefundCall.callService();
			
			Logger.verifyEquals("ok", portionRefundCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			MemberPoints mpdb = GodMemberWapper.selectMemberPointsByMemberId(memid).get(0);
			Logger.debug(FormatUtil.beanToGSONString(mpdb));
//			{"refundAmt":10.00,"refundNum":1,"memberId":"HY20200728010541","userId":501288492,"shopId":501288491,"availablePoints":5,"totalPoints":5,"orderNum":1,"orderAmt":101.00,"lastMonthOrderAmt":0.00,"lastOrderAmt":101.00,"lastOrderTime":"2020-07-28T11:24:05","lastOrderId":2037642528,"createTime":"2020-07-28T11:23:54","updateTime":"2020-07-28T11:23:54"}
			Logger.verifyEquals(0,mpdb.getAvailablePoints(), "验证getAvailablePoints");
			Logger.verifyEquals(shopid,mpdb.getShopId(), "验证getShopId");
			Logger.verifyEquals(10,mpdb.getRefundAmt().intValue(), "验证getRefundAmt");
			Logger.verifyEquals(1,mpdb.getRefundNum(), "验证getRefundNum");
			Logger.verifyEquals(0,mpdb.getTotalPoints(), "验证getTotalPoints");
			Logger.verifyNotNull(mpdb.getCreateTime(), "验证getCreateTime");
			List<PointsEntry> Entrylist = GodMemberWapper.selectPointsEntryByMemberid(memid);
			Logger.debug(FormatUtil.beanToGSONString(Entrylist));
			PointsEntry entrydb = Entrylist.stream().filter(x->{return x.getBizType().equals(110000);}).findFirst().get();
//			[{"entryId":2602,"memberId":"HY20200728010541","bizType":100000,"bizId":"2037642528","bizAmount":101.00,"changePoints":5,"points":5,"pointsTime":"2020-07-28T11:24:05","operator":"系统","remark":"下单送积分","originNo":"202007281124057890","createTime":"2020-07-28T11:23:54","updateTime":"2020-07-28T11:23:54"}]
			Logger.verifyEquals(-5,entrydb.getChangePoints(), "验证ChangePoints");
			Logger.verifyEquals(0,entrydb.getPoints(),  "验证points");
			Logger.verifyEquals("系统",entrydb.getOperator(),  "验证Operator");
			Logger.verifyEquals("退款扣积分",entrydb.getRemark(), "验证remark");        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
}